package part.d;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Semaphore;

import client.ClientConnection;

public class EchoServer {
	
	private static final int MAX_CONNECTIONS = 2;

	public static void main(String[] args) {
		try {
			ServerSocket socket = new ServerSocket(6969);
			Semaphore sem = new Semaphore(MAX_CONNECTIONS, true);
			
			System.out.println("Awaiting Connections...");
			while (true) {
				try {
					sem.acquire();
					Socket client = socket.accept();
					System.out.println("Connection Accepted");
					// give client connection semaphore so it can release when finished
					new Thread(new ClientConnection(client, sem)).start();
				} catch (InterruptedException ie) {
					System.err.println(ie);
				}
			}
		} catch (IOException ioe) {
			System.err.println(ioe);
		}
	}
}
